﻿////////////////////////////////////////////////////////////////////////////////////////////
// File: ParticleManager.cs                                                               //
// Author: Mike Binnix                                                                    //
// Desc: Class to manage applying all acting forces on each particle and updating the     //
//       particles that are active in the simulation.                                     //
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace SlothEngine.Physics
{
    public class ParticleManager
    {
        private List<ParticleForce> particleList;

        public ParticleManager()
        {
            particleList = new List<ParticleForce>();           
        }

        /// <summary>
        /// Adds a new ParticleForce to the manager.
        /// </summary>
        /// <param name="particleForce">ParticleForce to add.</param>
        public void Add(ParticleForce particleForce)
        {
            particleList.Add(particleForce);
        }
        /// <summary>
        /// Creates a new ParticleForce from the given particle and force and then adds it to the manager.
        /// </summary>
        /// <param name="particle">Particle to add.</param>
        /// <param name="forceGenerator">Force that acts on the particle.</param>
        public void Add(XNAParticle particle, IPForceGenerator forceGenerator)
        {
            ParticleForce tmp = new ParticleForce(particle, forceGenerator);

            particleList.Add(tmp);
        }
        /// <summary>
        /// Removes the given ParticleForce from the manager.  Throws an exception if remove failed.
        /// </summary>
        /// <param name="particleForce">ParticleForce to remove.</param>
        public void Remove(ParticleForce particleForce)
        {
            if (!particleList.Remove(particleForce))
                throw new Exception("Could not find item to be removed.");
        }
        /// <summary>
        /// Removes the ParticleForce for the given partilce and force generator from the manager.
        /// Throws an exception if the remove failed.
        /// </summary>
        /// <param name="particle">Particle to remove.</param>
        /// <param name="forceGenerator">Force that acts on the particle.</param>
        public void Remove(XNAParticle particle, IPForceGenerator forceGenerator)
        {
            ParticleForce tmp = new ParticleForce(particle, forceGenerator);

            if (!particleList.Remove(tmp))
                throw new Exception("Could not find item to be removed.");
        }
        /// <summary>
        /// Clears the ParticleForce list of all objects.
        /// </summary>
        public void Clear()
        {
            particleList.Clear();
        }
        /// <summary>
        /// Applies the force generators to each particle and then updates each particle.
        /// </summary>
        /// <param name="gameTime">Current game time.</param>
        public void Update(GameTime gameTime)
        {
            // Update forces acting on particles
            for (int i = 0; i < particleList.Count; i++)
            {
                ParticleForce pf = new ParticleForce();
                pf = particleList[i];

                // only update force generator if there is one assigned to the particle
                if (pf.ForceGenerator != null)
                {
                    pf.ForceGenerator.Update(gameTime, pf.Particle);
                    particleList[i] = pf;
                }
            }

            // Update particles
            for (int i = 0; i < particleList.Count; i++)
            {
                particleList[i].Particle.Update(gameTime);

                // Remove particle if destroy is true
                if (particleList[i].Particle.Destroy)
                    particleList.RemoveAt(i);
            }
        }
    }
}
